//-----------------------------------------------------------------------
// <copyright file="GiftDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class GiftDAOBase
    {
        #region Common methods
        public virtual Gift CreateGiftFromReader(IDataReader reader)
        {
            Gift item = new Gift();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("GiftId"))) item.GiftId = (long)reader["GiftId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) item.Url = (string)reader["Url"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThumbnailImage"))) item.ThumbnailImage = (string)reader["ThumbnailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatId"))) item.CatId = (long)reader["CatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (int)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Marks"))) item.Marks = (int)reader["Marks"];
                if (!reader.IsDBNull(reader.GetOrdinal("Quantity"))) item.Quantity = (long)reader["Quantity"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateGiftFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateGift methods
            
        public virtual void CreateGift(Gift gift)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftInsert");
                
                database.AddInParameter(dbCommand, "@Url", DbType.String, gift.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, gift.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, gift.Summary);
                database.AddInParameter(dbCommand, "@Description", DbType.String, gift.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, gift.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, gift.UpdatedDate);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, gift.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, gift.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, gift.ThumbnailImage);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, gift.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int32, gift.Viewed);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, gift.StatusId);
                database.AddInParameter(dbCommand, "@Marks", DbType.Int32, gift.Marks);
                database.AddInParameter(dbCommand, "@Quantity", DbType.Int64, gift.Quantity);
                database.AddOutParameter(dbCommand, "@GiftId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                gift.GiftId = (long)database.GetParameterValue(dbCommand, "@GiftId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateGiftException, ex);
            }
        }

        #endregion

        #region UpdateGift methods
        
        public virtual void UpdateGift(Gift gift)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftUpdate");            
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int64, gift.GiftId);
                database.AddInParameter(dbCommand, "@Url", DbType.String, gift.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, gift.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, gift.Summary);
                database.AddInParameter(dbCommand, "@Description", DbType.String, gift.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, gift.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, gift.UpdatedDate);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, gift.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, gift.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, gift.ThumbnailImage);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, gift.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int32, gift.Viewed);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, gift.StatusId);
                database.AddInParameter(dbCommand, "@Marks", DbType.Int32, gift.Marks);
                database.AddInParameter(dbCommand, "@Quantity", DbType.Int64, gift.Quantity);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateGiftException, ex);
            }
        }
        
        #endregion

        #region DeleteGift methods
        public virtual void DeleteGift(long giftId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftDelete");
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int64, giftId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteGiftException, ex);
            }
        }

        #endregion

        #region GetGift methods
        
        public virtual Gift GetGift(long giftId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftGet");
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int64, giftId);
                
                Gift gift =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        gift = CreateGiftFromReader(reader);
                        reader.Close();
                    }
                }
                return gift;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGiftException, ex);
            }
        }
        
        #endregion

        #region GetGiftList methods
        public virtual GiftCollection GetGiftList(GiftColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GiftCollection giftCollection = new GiftCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Gift gift = CreateGiftFromReader(reader);
                        giftCollection.Add(gift);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return giftCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGiftListException, ex);
            }
        }
        
        public virtual GiftCollection GetGiftList(GiftColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetGiftList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetGiftList methods
        public virtual GiftCollection GetGiftListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GiftCollection giftCollection = new GiftCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Gift gift = CreateGiftFromReader(reader);
                        giftCollection.Add(gift);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return giftCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGiftListException, ex);
            }
        }
        
        public virtual GiftCollection GetGiftListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetGiftListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}